'''
Created on 2014年9月4日

@author: loseair123
'''

'''
class Student(object):
    
    __slots__ = ('name', 'age')
    
s1=Student()
s1.name='eko'
s1.age='22'
s1.sex='male'
#动态为实例绑定方法
'''
from types import MethodType
class Student(object):
    __slots__ = ('height','__weight','__name','__age','__sex')
    def __init__(self,name,age,sex):
        self.__name=name
        self.__age=age
        self.__sex=sex
   

eko=Student('eko',22,'Male')
eko.job='student'


#为实例动态绑定方法 
def setName(self,name):
    self.name=name
eko.setName=MethodType(setName,Student)
eko.setName('Mr.Eko')

def setWeight(self,weight):
    self.weight=weight
eko.setWeight=MethodType(setWeight,Student)
eko.setWeight(60)
print(eko)

frank=Student('frank',22,'Male')
#frank.setWeight(20)

#为类动态绑定方法
Student.setName=MethodType(setName,Student)
frank.setName('Frank')
print(frank.name)


#防止类动态添加属性 只需要添加 slot关键词就行  __slots__ = ('name', 'age')只有name和age可以被动态绑定属性

